//
//  SSSGuideView.m
//  SSSMerchant
//
//  Created by Darker on 2017/3/24.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSGuideView.h"


#define DDHidden_TIME   1.5

#define kApplicationHasShowIntroViewKey @"ApplicationHasShowIntroViewKey"
#define kApplicationVersion             @"ApplicationVersion"

@interface SSSGuideView()<UIScrollViewDelegate>
{
    BOOL isGetin; // 是否再次滑动进入app
}

@property (nonatomic, strong) NSArray       *imageArray;
@property (nonatomic, strong) UIPageControl *imagePageControl;
/** scrollView */
@property (nonatomic,strong)  UIScrollView *guidePageView;
@property (nonatomic, strong) UIButton *skipButton;

@end

@implementation SSSGuideView

- (instancetype)dh_initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray buttonIsHidden:(BOOL)isHidden {
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        if (isHidden == YES) {
            self.imageArray = imageArray;
        }
        
        // 设置引导视图的scrollview
        _guidePageView = [[UIScrollView alloc]initWithFrame:frame];
        [_guidePageView setBackgroundColor:[UIColor colorWithRed:0.2275 green:0.6118 blue:0.702 alpha:1.0]];
        [_guidePageView setContentSize:CGSizeMake(KSCREEN_Width*imageArray.count, KSCREEN_Height)];
        [_guidePageView setBounces:NO];
        [_guidePageView setPagingEnabled:YES];
        [_guidePageView setShowsHorizontalScrollIndicator:NO];
        [_guidePageView setDelegate:self];
        [self addSubview:_guidePageView];
        // 设置引导页上的跳过按钮
        _skipButton = [[UIButton alloc]initWithFrame:CGRectMake(KSCREEN_Width*0.8, KSCREEN_Height*0.15, 50, 25)];
        [_skipButton setTitle:@"跳过" forState:UIControlStateNormal];
        [_skipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_skipButton setBackgroundColor:[UIColor colorWithHex:@"aaadaf"]];
        [_skipButton.layer setCornerRadius:5.0];
        [_skipButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_skipButton];
        
        
        // 添加在引导视图上的多张引导图片
        for (int i=0; i<imageArray.count; i++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(KSCREEN_Width*i, 0, KSCREEN_Width, KSCREEN_Height)];
            imageView.image = imageArray[i];
            [_guidePageView addSubview:imageView];
            
            // 设置在最后一张图片上显示进入体验按钮
            if (i == imageArray.count-1 && isHidden == YES) {
                [imageView setUserInteractionEnabled:YES];
                
                UIButton *startButton = [[UIButton alloc]initWithFrame:CGRectMake(KSCREEN_Width*0.3, KSCREEN_Height*0.85, 156, 35)];
                startButton.centerX = self.centerX;
                //                [startButton setTitle:@"立即体验" forState:UIControlStateNormal];
                [startButton setTitleColor:[UIColor colorWithRed:164/255.0 green:201/255.0 blue:67/255.0 alpha:1.0] forState:UIControlStateNormal];
                [startButton.titleLabel setFont:[UIFont systemFontOfSize:21]];
                [startButton setTitle:@"立即体验" forState:UIControlStateNormal];
                //                [startButton setBackgroundImage:[UIImage imageNamed:@"立即体验"] forState:UIControlStateNormal];
                [startButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];               [imageView addSubview:startButton];
            }
        }
        
        // 设置引导页上的页面控制器
        self.imagePageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(KSCREEN_Width*0.0, KSCREEN_Height*0.77, KSCREEN_Width*1.0, KSCREEN_Height*0.1)];
        self.imagePageControl.currentPage = 0;
        self.imagePageControl.numberOfPages = imageArray.count;
        self.imagePageControl.pageIndicatorTintColor = ZDCommonBgColor;
        self.imagePageControl.currentPageIndicatorTintColor = ZDCommonBlueColor;
        [self addSubview:self.imagePageControl];
        
    }
    return self;
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollview {
    int page = scrollview.contentOffset.x / scrollview.frame.size.width;
    [self.imagePageControl setCurrentPage:page];
    if (self.imageArray && page == self.imageArray.count-1 && isGetin == YES) {
        [self buttonClick:nil];
        isGetin = NO;
        return;
    }
    if (self.imageArray && page == self.imageArray.count-1) {
        isGetin = YES;
    }else{
        isGetin = NO;
    }
}

- (void)buttonClick:(UIButton *)button {
    [UIView animateWithDuration:DDHidden_TIME animations:^{
        self.alpha = 0;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(DDHidden_TIME * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self performSelector:@selector(removeGuidePageHUD) withObject:nil afterDelay:1];
        });
    }];
}

- (void)removeGuidePageHUD {
    [self removeFromSuperview];
    
}


@end
